package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.ContactaddressForm;
import com.vcaresolution.logistic.to.ContactaddressTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>Contact addressController</code> is used to track all login related actions
 *
 * @author Dharam C. Gusai
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class ContactaddressController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.CONTACTADDRESSFORM_MODELATTRIBUTE)
	public ContactaddressForm createForm(){
		return new ContactaddressForm();
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDContact Address Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_CONTACTADDRESS_URL,method = RequestMethod.GET)
	public ModelAndView loadAddContactaddress(@RequestParam(required = true, value = "contactId") final String contactId) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_CONTACTADDRESS_VIEW);
		ContactaddressForm contactaddressForm = new ContactaddressForm();
		contactaddressForm.setContactid(Integer.parseInt(contactId));
		
		modelAndView.addObject(ApplicationConstants.CONTACTADDRESSFORM_MODELATTRIBUTE, contactaddressForm);
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDContact Address Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_CONTACTADDRESS_URL,method = RequestMethod.POST)
	public ModelAndView processAddContactaddress(@Valid ContactaddressForm contactaddressForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			contactaddressForm.setUser(getLoggedInUser(request));
			
			getServiceRegistry().getContactaddressService().add(contactaddressForm);
			
			//modelAndView = list(""+contactaddressForm.getContactid());
			
			RedirectView redirectView = new RedirectView();
			redirectView.setUrl(ApplicationConstants.LIST_CONTACTADDRESS_URL+"?contactId="+contactaddressForm.getContactid());
			redirectView.setContextRelative(true);
			
			modelAndView.setView(redirectView);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListContact Address Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_CONTACTADDRESS_URL,method = RequestMethod.GET)
	public ModelAndView list(@RequestParam(required = true, value = "contactId") final String contactId) {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<ContactaddressTO> contactaddressList = getServiceRegistry().getContactaddressService().findByAll(Integer.parseInt(contactId));
		
		modelAndView.addObject(ApplicationConstants.CONTACTADDTRSSLIST_MODELATTRIBUTE, contactaddressList);
		modelAndView.setViewName(ApplicationConstants.LIST_CONTACTADDRESS_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDContact Address Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_CONTACTADDRESS_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditContactaddress(@RequestParam(value="contactId") String contactID) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object contactaddressObject = getServiceRegistry().getContactaddressService().findById(Integer.parseInt(contactID));
		
		modelAndView.addObject(ApplicationConstants.CONTACTADDRESSFORM_MODELATTRIBUTE, contactaddressObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_CONTACTADDRESS_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDContact Address Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_CONTACTADDRESS_URL,method = RequestMethod.POST)
	public ModelAndView processEditContactaddress(@Valid ContactaddressForm contactaddressForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			contactaddressForm.setUser(getLoggedInUser(request));
			
			getServiceRegistry().getContactaddressService().update(contactaddressForm);
			
			modelAndView = list(""+contactaddressForm.getContactid());
		}
		
		return modelAndView;
	}
}